package edu.mtu.citizenscience.ethnography;

import java.util.List;

import edu.mtu.citizenscience.ethnography.database.Interview;
import edu.mtu.citizenscience.ethnography.database.InterviewPhoto;
import edu.mtu.citizenscience.ethnography.util.DataServiceActivity;
import edu.mtu.citizenscience.ethnography.util.Util;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class PhotoGalleryActivity extends DataServiceActivity {

	private Interview interview;
	private int interviewID;

	private GridView photoGrid;

	private ImageAdapter adapter;

	private static final String LOG_TAG = "PhotoGalleryActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(LOG_TAG, "PhotoGalleryActivity is being created");
		setContentView(R.layout.activity_photo_gallery);

		photoGrid = (GridView) findViewById(R.id.photoGrid);

		if (savedInstanceState == null) {
			Bundle extras = getIntent().getExtras();
			if (extras != null) {
				interviewID = extras.getInt(Util.ID_KEY, -1);
				if (interviewID == -1) {
					fail(R.string.activity_start_error);
				}
			} else {
				fail(R.string.activity_start_error);
			}
		} else {
			interviewID = savedInstanceState.getInt(Util.ID_KEY);
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		Log.d(LOG_TAG, "PhotoGalleryActivity is starting");
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putInt(Util.ID_KEY, interviewID);
		super.onSaveInstanceState(outState);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_photo_gallery, menu);
		return true;
	}

	@Override
	protected void performDatabaseOperation() {
		interview = getService().getInterview(interviewID);
		if (interview == null) {
			fail(R.string.activity_start_error);
		}

		List<InterviewPhoto> photos = getService().photoFilesForInterview(interview);
		adapter = new ImageAdapter(this, photos);
		photoGrid.setAdapter(adapter);

		photoGrid.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				InterviewPhoto photo = (InterviewPhoto) adapter.getItem(position);
				Intent intent = new Intent(PhotoGalleryActivity.this, ImageViewActivity.class);
				intent.putExtra(Util.ID_KEY, interviewID);
				// Note: this should work because the value returned from the File getName()
				// and the InterviewPhoto getFileName() (which is package-private) should
				// be the same.
				intent.putExtra(Util.PHOTO_KEY, photo.getPhotoFile().getName());
				startActivityForResult(intent, 0);
			}
		});
	}

	private class ImageAdapter extends BaseAdapter {
		private Context context;
		private List<InterviewPhoto> photos;

		public ImageAdapter(Context context, List<InterviewPhoto> photos) {
			this.context = context;
			this.photos = photos;
		}

		@Override
		public int getCount() {
			return photos.size();
		}

		@Override
		public Object getItem(int position) {
			return photos.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			System.gc();
			ImageView imageView;
			if (convertView == null) {
				// XXX Make more robust to support multiple devices
				imageView = new ImageView(context);
				imageView.setLayoutParams(new GridView.LayoutParams(128, 128));
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setPadding(8, 8, 8, 8);

				Bitmap bitmap = Util.decodeSampledBitmapFromFile(
						photos.get(position).getPhotoFile().getAbsolutePath(), 
						128, 
						128);

				imageView.setImageBitmap(bitmap);
			} else {
				imageView = (ImageView) convertView;
			}

			return imageView;
		}

	}
	
	
}
